package com.example.myapp;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.myapp.webView.ui.WebViewToHtmlActivity;
import com.example.myapp.webView.ui.WebViewToUrlActivity;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	Button btn_main_webView_url, btn_main_webView_html;
    private static DemoHandler handler;
    String info;
    TextView tv_main_name;
    ImageView iv_main;
	ImageLoader imageLoader = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
        if (handler == null) {
            handler = new DemoHandler();
        }
        Bundle bundle=getIntent().getExtras();
        info=bundle.getString("info");
		initView();
		getJson();
	}

	private void initView() {
		// TODO Auto-generated method stub
		tv_main_name=(TextView) findViewById(R.id.tv_main_name);
		iv_main=(ImageView) findViewById(R.id.iv_main);
		btn_main_webView_html = (Button) findViewById(R.id.btn_main_webView_html);
		btn_main_webView_url = (Button) findViewById(R.id.btn_main_webView_url);
		btn_main_webView_url.setOnClickListener(this);
		btn_main_webView_html.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent = new Intent();
		switch (v.getId()) {
		case R.id.btn_main_webView_url:
			intent.setClass(MainActivity.this, WebViewToUrlActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.btn_main_webView_html:
			intent.setClass(MainActivity.this, WebViewToHtmlActivity.class);
			startActivity(intent);
			finish();
			break;
		default:
			break;
		}
	}
    public static void sendMessage(Message msg) {
        handler.sendMessage(msg);
    }
    public class DemoHandler extends Handler {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    Uri uri = Uri.parse("http://www.baidu.com");
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                    break;

                case 1:

                    break;
            }
        }
    }
    private void getJson(){
		imageLoader = ImageLoader.getInstance();
    	try {
			JSONObject jsonObject=new JSONObject(info);
			String url=jsonObject.getString("figureurl_qq_2");
			String name=jsonObject.getString("nickname");
			tv_main_name.setText(name);
			imageLoader.displayImage(url, iv_main);
			Toast.makeText(MainActivity.this, "url--->>【"+url+"】name--->>【"+name+"】", Toast.LENGTH_SHORT).show();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
}
